import 'package:flutter/material.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter_juwo/ui/custom/LoadingDialog.dart';
import 'package:flutter_juwo/ui/utils/progress_view.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';

class UiUtils {
  static Widget pushHomeAppBar(
      BuildContext context, String title, String headUrl, String name) {
    return new AppBar(
      backgroundColor: Colours.white,
      elevation: 0.0,
      brightness: Brightness.light,
      title: Text('$title',
          style: TextStyle(
              fontSize: ScrUtil.sp(36.0),
              fontFamily: Font.PingFangBlod,
              color: Colours.appC1)),
      actions: <Widget>[
        Container(
          //padding: UiUtils.edgeOnly(left: 10, right: 10, top: 10, bottom: 10),
          child: ClipOval(
            child: CachedNetworkImage(
              width: ScrUtil.w(50),
              height: ScrUtil.w(50),
              fit: BoxFit.fill,
              imageUrl: headUrl,
              placeholder: (context, url) => new ProgressView(),
              errorWidget: (context, url, error) => Image.asset(
                Utils.getImgPath('icon_default_head'),
                fit: BoxFit.fill,
              ),
            ),
          ),
        ),
        Container(
          alignment: Alignment.center,
          padding: UiUtils.edgeOnly(l: 30, r: 30, t: 30, b: 30),
          child: UiUtils.t(name),
        )
      ],
    );
  }

  static Widget myAppBar(BuildContext context, String title) {
    return new AppBar(
        backgroundColor: Colours.white,
        elevation: 0.0,
        brightness: Brightness.light,
        centerTitle: true,
        title: Text('$title',
            style: TextStyle(
                fontSize: ScreenUtil().setSp(34),
                fontFamily: Font.PingFangRegular,
                color: Colours.appC1)));
  }

  ///appBar
  static Widget appBar(BuildContext context, String title) {
    return AppBar(
      brightness: Brightness.light,
      backgroundColor: Colours.white,
      title: Text(
        '$title',
        style: TextStyle(
            fontSize: ScreenUtil().setSp(34),
            fontFamily: Font.PingFangBlod,
            color: Colours.appC1),
      ),
      centerTitle: true,
      elevation: 0.0,
      leading: InkWell(
          highlightColor: Color(0xFFB3E5FC),
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            padding: EdgeInsets.fromLTRB(12.0, 0.0, 12.0, 0.0),
            child: Image.asset(
              Utils.getImgPath('return_left'),
              width: 18.0,
              height: 20.0,
            ),
          )),
    );
  }

  ///appBar
  static Widget appHomeBar(BuildContext context, String title) {
    return new AppBar(
      backgroundColor: Colours.white,
      centerTitle: true,
      brightness: Brightness.light,
      elevation: 0.0,
      title: Text(
        '$title',
        style: TextStyle(
            fontSize: ScreenUtil().setSp(36),
            fontFamily: Font.PingFangBlod,
            color: Colours.cText0),
      ),
    );
  }

  ///分割线
  static Container viewDivider = Container(
    height: ScreenUtil().setHeight(ScreenUtil().setHeight(1.0)),
    color: Colours.appC7,
  );

  ///可以改变高度的分割线
  /// height  0.5 或者 10
  static Widget dividerWidget(double height) {
    return Container(
      height: height,
      color: Colours.c243,
    );
  }

  ///可以改变高度的分割线
  /// height  0.5 或者 10
  static Widget divider({double height = 1, Color color = Colours.c243}) {
    return Container(
      height: ScreenUtil().setWidth(height),
      color: color,
    );
  }

  static Widget dividerLine() {
    return Container(
      height: 0.5,
      color: Colours.appC7,
    );
  }

  static Widget cachedNetworkImage(double width, double height, String url) {
    return CachedNetworkImage(
      width: ScreenUtil().setWidth(width),
      height: ScreenUtil().setWidth(height),
      fit: BoxFit.fill,
      imageUrl: url,
      placeholder: (context, url) => new ProgressView(),
      errorWidget: (context, url, error) => new Icon(
        Icons.error,
        color: Colors.redAccent,
      ),
    );
  }

  static Widget bottomBotton(String text, Function fun,
      {bool clickAble = true}) {
    return GestureDetector(
      onTap: () {
        fun();
      },
      child: Container(
        height: ScreenUtil().setWidth(88),
        width: ScreenUtil().setWidth(690),
        decoration: BoxDecoration(
          color: clickAble ? Colours.appC16 : Colours.appC4, // 底色
          borderRadius: new BorderRadius.circular(20), // 圆角度 steelseries
          //  borderRadius: new BorderRadius.vertical(top: Radius.elliptical(20, 50)), // 也可控件一边圆角大小
        ),
        alignment: Alignment.center,
        child: Text(
          "$text",
          style: TextStyles.custom(28, Colours.white, Font.PingFangMedium),
        ),
      ),
    );
  }

  static void showMyDialog(BuildContext context, {String text = "加载中..."}) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return LoadingDialog(
            text: text,
          );
        });
  }

  static Widget myTextField1(
      BuildContext context, TextEditingController controller,
      {int maxLines = 1, String hintText = ""}) {
    return Theme(
      data: Theme.of(context).copyWith(splashColor: Colors.blue),
      child: TextField(
        autofocus: false,
        maxLines: maxLines,
        enableInteractiveSelection: true,
        controller: controller,
        style:
            TextStyle(fontSize: ScreenUtil().setSp(30), color: Colours.appC2),
        textAlign: TextAlign.end,
        decoration: InputDecoration(
          filled: true,
          fillColor: Colours.c243,
          hintText: hintText,
          hintStyle: TextStyles.custom(30, Colours.appC4, Font.PingFangRegular),
          contentPadding:
              const EdgeInsets.only(left: 14.0, bottom: 8.0, top: 8.0),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
            borderRadius: BorderRadius.circular(8),
          ),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
            borderRadius: BorderRadius.circular(8),
          ),
        ),
      ),
    );
  }

  static Widget myTextField(
      BuildContext context, TextEditingController controller,
      {int maxLines = 1, String hintText = ""}) {
    return Theme(
      data: Theme.of(context).copyWith(splashColor: Colors.blue),
      child: TextField(
        autofocus: false,
        maxLines: maxLines,
        enableInteractiveSelection: true,
        controller: controller,
        style: TextStyle(
            fontSize: ScreenUtil().setWidth(34), color: Colours.appC1),
        decoration: InputDecoration(
          filled: true,
          fillColor: Colours.c243,
          hintText: hintText,
          hintStyle: TextStyles.custom(34, Colours.appC4, Font.PingFangRegular),
          contentPadding:
              const EdgeInsets.only(left: 14.0, bottom: 8.0, top: 8.0),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
            borderRadius: BorderRadius.circular(8),
          ),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
            borderRadius: BorderRadius.circular(8),
          ),
        ),
      ),
    );
  }

  static void dismissMyDialog(BuildContext context) {
    NavigatorUtils.goBack(context);
  }

  //text
  static t(String text,
      {double size = 30,
      Color color = Colours.appC2,
      String font = Font.PingFangMedium,
      int maxLines = 1,
      TextOverflow overflow = TextOverflow.ellipsis}) {
    return Text(
      text,
      maxLines: maxLines,
      overflow: TextOverflow.ellipsis,
      textAlign: TextAlign.left,
      style: custom(size: size, color: color, font: font),
    );
  }

  ///传入样式
  static TextStyle custom(
      {double size = 30,
      Color color = Colours.appC2,
      String font = Font.PingFangMedium}) {
    return TextStyle(
        fontSize: ScrUtil.sp(size), color: color, fontFamily: font);
  }

  ///设置边界
  static EdgeInsets edgeOnly(
      {double l = 0, double t = 0, double r = 0, double b = 0}) {
    return EdgeInsets.fromLTRB(
        ScrUtil.w(l), ScrUtil.w(t), ScrUtil.w(r), ScrUtil.w(b));
  }
}
